<template>
  <div>
    <Button type="primary" class="back-button" @click="$router.push({name:'calculation'})" icon="md-arrow-back">返回上一级</Button>
    <Card>
      <Row>
        <Table stripe border :columns="tableColumnsShow" :data="tableData"></Table>
      </Row>
    </Card>
    <Card class="m-t-sm">
      <Table stripe border :loading="loading" :columns="rankColumns" :data="rankData"></Table>
      <page-temp :total="total" :current="page.pageNo" :size="page.pageSize" @changePage='changePage'></page-temp>
      <Button type="success" @click="exportExcel">导出EXCEL</Button>
    </Card>
  </div>
</template>

<script>
import dictSpan from "_c/dict/dict-span.vue";
import { getDict } from "@/api/configure";
import Api from "@/api/calculation";
const api = new Api();
// 赛事管理
export default {
  components: {
    dictSpan
  },
  data() {
    return {
      loading: true,
      model: {},
      rankId: this.$route.params.id,
      page: {
        rankId: this.$route.params.id,
        pageSize: this.$config.PAGE_SIZE,
        pageNo: 1
      },
      AGOUTS: [],
      rankColumns: [
        {
          title: "本周排名",
          key: "showRank",
          align: "left",
          render: (h, params) => {
            return h(
              "span",
              {
                style: {
                  color: this.$config.getColor(
                    params.row.showRank,
                    params.row.lastRank
                  )
                }
              },
              params.row.showRank
            );
          }
        },
        {
          title: "上周排名",
          key: "lastRank",
          align: "left",
          render: (h, params) => {
            return h(
              "span",
              params.row.lastRank == null ? "--" : params.row.lastRank
            );
          }
        },
        {
          title: "选手名称",
          key: "userName",
          align: "left",
          render: (h, params) => {
            return h(
              "span",
              this.$config.userName(params.row.userName, params.row.englishName)
            );
          }
        },
        {
          title: "国籍",
          key: "country",
          align: "left",
          render: (h, params) => {
            let self = this;
            let row = self.AGOUTS.find(x => x.code === params.row.country) || {
              name: params.row.country
            };
            return h("span", row.name);
          }
        },
        {
          title: "参赛场次",
          key: "frequency",
          align: "left",
          render: (h, params) => {
            return h(
              "span",
              (params.row.frequency == null ? 0 : params.row.frequency) + "场"
            );
          }
        },
        {
          title: "积分",
          key: "score",
          align: "left",
          render: (h, params) => {
            return h("span", this.$config.toIntegral(params.row.score));
          }
        },
        {
          title: "操作",
          key: "action",
          width: 230,
          align: "left",
          render: (h, params) => {
            let self = this;
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      self.$router.push({
                        path: `/integral-calculation-management/calculation/person-detailed/${
                          params.row.detailId
                        }`
                      });
                    }
                  }
                },
                "查看比赛明细"
              )
            ]);
          }
        }
      ],

      rankData: [],
      total: 0,

      tableColumnsShow: [
        {
          title: "计算规则",
          key: "kindRule",
          align: "left",
          render: (h, params) => {
            let self = this;
            return h(dictSpan, {
              props: {
                code: "JSGZ",
                value: self.model.kindRule
              }
            });
          }
        },
        {
          title: "排名周期",
          key: "startCountcycletime",
          align: "left",
          render: (h, params) => {
            let self = this;
            return h(
              "span",
                "第" +params.row.rankWeek+"周，"+
                self.$moment(params.row.endCountcycletime).format("YYYY-MM-DD")
            );
          }
        },
        {
          title: "选手数量",
          key: "athleteNum",
          align: "left"
        },
        {
          title: "操作者",
          key: "handlers",
          align: "left"
        },
        {
          title: "计算时间",
          key: "countTime",
          align: "left",
          render: (h, params) => {
            let self = this;
            return h(
              "span",
              this.$moment(params.row.countTime).format("YYYY-MM-DD HH:mm:ss")
            );
          }
        }
      ],
      tableData: []
    };
  },
  mounted() {
    let self = this;
    self.init();
    getDict({ code: "AG_OUT" }).then(data => {
      self.AGOUTS = data;
      self.changePage(1);
    });
  },
  methods: {
    // 初始化
    init() {
      let self = this;
      api.rankDetailOne(self.rankId).then(data => {
        self.model = data;
        self.tableData = [self.model];
      });
    },
    // 查询计算结果
    fetchList() {
      let self = this;
      self.loading = true;
      api
        .rankDetail(self.page)
        .then(data => {
          self.rankData = data.list || [];
          self.total = data.count || 0;
          self.loading = false;
        })
        .catch(error => {
          self.loading = false;
        });
    },
    // 分页
    changePage(pageNum) {
      this.page.pageNo = pageNum;
      this.fetchList();
    },
    exportExcel() {
      let self = this;
      api.exportExcel({ rankId: self.rankId }).then(fileUrl => {
        let link = document.createElement("a");
        link.href = fileUrl;
        // let suffix = fileUrl.substr(fileUrl.lastIndexOf("."));
        // link.download = self.detail.eventsName + "场次安排" + suffix;
        link.click();
      });
    }
  }
};
</script>

<style scoped>
</style>
